//
//  JLDBBasicViewController.m
//  Estate
//
//  Created by wangdongdong on 16/7/30.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "JLDBBasicViewController.h"
#import "TableTopStateCell.h"
#import "QianDTableCell.h"
#import "DefaineCell.h"
#import "C_wladdCell_1.h"
#import "B_wuLiaoCell.h"

@interface JLDBBasicViewController ()

@end

@implementation JLDBBasicViewController

- (UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-104)style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.showsVerticalScrollIndicator=NO;
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"C_wladdCell_1" bundle:nil] forCellReuseIdentifier:@"doubleCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"defaine"];
        [_tableView registerNib:[UINib nibWithNibName:@"TableTopStateCell" bundle:nil] forCellReuseIdentifier:@"firstCell"];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [super viewDidLoad];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) {
        return 7;
    } else if (section == 1) {
        return 3;
    } else if (section == 2) {
        return 4;
    } else {
        return 1;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        if(row == 0){
            return 30;
        }
        return 44;
    }else if(section==1){
        if(row==0){
            return 44;
        }else{
            return 100;
        }
    }else if (section == 2){
        return 44;
    } else {
        return 100;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0.001;
    }else{
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //组
    long section=indexPath.section;
    //行
    long row=indexPath.row;
    if (section==0) {
        if (row == 0) {
            TableTopStateCell * celld = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
            for (id obj in celld.contentView.subviews) {
                [obj removeFromSuperview];
            }
            UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake((DEF_SCREEN_WIDTH - 100)/2, 0, 100, 30)];
            UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 14, 14)];
            [btn addSubview:imgView];
            imgView.image = [UIImage imageNamed:@"chulizhong"];
            UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 70, 30)];

            // 跟进单
            if (self.pRecordM.PROCESS_TYPE == 1) {
                if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_REVIEW) {
                    [lab setText:@"状态:待审阅"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_REVIEW_PASS_NO) {
                    [lab setText:@"状态:审阅不通过"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_CHECK) {
                    [lab setText:@"状态:待复查"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_CHECK_PASS_YES) {
                    [lab setText:@"状态:复查通过"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_END) {
                    [lab setText:@"状态:结束"];
                }
                // 督办单
            } else if (self.pRecordM.PROCESS_TYPE == 2) {
                if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_CHECK) {
                    [lab setText:@"状态:待复查"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_CHECK_PASS_YES) {
                    [lab setText:@"状态:复查通过"];
                    
                } else if (self.pRecordM.PROCESS_STAGE == PROCESS_STAGE_END) {
                    [lab setText:@"状态:结束"];
                }
            }
            
            [lab setFont:[UIFont systemFontOfSize:12]];
            [btn addSubview:lab];
            [celld.contentView addSubview:btn];
            celld.backgroundColor = [UIColor colorWithRed:0.7 green:0.97 blue:0.97 alpha:1.00];
            return celld;
        } else if (row==1) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"项  目:";
            cell.text.text=[LSUtility getProjectNameByProjectId:self.pRecordM.PRO_ID][@"PROJECT_NAME"];
            return cell;
        } else if (row==2) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"时  间:";
            cell.text.text=self.pRecordM.CREATE_TIME;
            return cell;
        } else if (row==3) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"类  型:";
            if (self.pRecordM.PROBLEM_TYPE == PROBLEM_TYPE_SCOUT) {
                cell.text.text = @"巡视";
            } else if (self.pRecordM.PROBLEM_TYPE == PROBLEM_TYPE_SITE) {
                cell.text.text = @"旁站";
            } else if (self.pRecordM.PROBLEM_TYPE == PROBLEM_TYPE_SAFE) {
                cell.text.text = @"安全";
            } else if (self.pRecordM.PROBLEM_TYPE == PROBLEM_TYPE_OTHER) {
                cell.text.text = @"其他";
            }
            return cell;
        } else if(row==4) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"位  置:";
            NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.addressInfo];
            cell.text.text = [NSString stringWithFormat:@"%@ %@",
                                   DEFISNUL(dic[@"address"]),
                                   DEFISNUL(dic[@"leftDistanceStr"])];
            
            return cell;
        } else if (row==5) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"发送形式:";
            if (self.pRecordM.SEND_TYPE == SEND_TYPE_CONTACTLIST) {
                cell.text.text = @"工作联系单";
            } else if (self.pRecordM.SEND_TYPE == SEND_TYPE_SUPERVISIONNOTICE) {
                cell.text.text = @"监理通知单";
            } else if (self.pRecordM.SEND_TYPE == SEND_TYPE_STOPLIST) {
                cell.text.text = @"工程暂停令";
            } else if (self.pRecordM.SEND_TYPE == SEND_TYPE_NULL) {
                cell.text.text = @"无";
            }
            return cell;
        } else {
            C_wladdCell_1 *cell=[tableView dequeueReusableCellWithIdentifier:@"doubleCell" forIndexPath:indexPath];
            cell.line.hidden=YES;
            cell.leftConstraint.constant=0.;
            cell.title1.text=@"记录人:";
            cell.title2.text=@"负责人:";
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            NSDictionary *dic=[self queryProcessByProcessID:self.pRecordM.PROCESS_ID];
            cell.text1.text = [LSUtility getuserIdByUserName:dic[@"PROCESS_PUBLISHERS"]][@"REAL_NAME"];
//            cell.text1.text = [LSUtility getuserIdByUserName:self.pRecordM.RECORD_USER][@"REAL_NAME"];
            cell.text2.text = [LSUtility getuserIdByUserName:self.pRecordM.RECORD_MASTER][@"REAL_NAME"];
            cell.text1.userInteractionEnabled = NO;
            cell.text2.userInteractionEnabled = NO;
            return cell;
        }
    } else if (section==1) {
        if (row==0) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"标  题:";
            cell.textField.text = self.pRecordM.PROCESS_TITLE;
            cell.isedit = NO;
            return cell;
        } else if(row==1) {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"描  述:";
            cell.text.text = self.pRecordM.PROCESS_CONTENT;
            cell.isEdit = NO;
            return cell;
        } else {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.title.text=@"要  求:";
            cell.text.text = self.pRecordM.PROCESS_REQUIRED;
            cell.isEdit = NO;
            return cell;
        }
    } else if (section == 2) {
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
        cell.line.hidden=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        if(row==0){
            cell.title.text=@"时  限:";
            cell.text.text = self.pRecordM.END_TIME;
            return cell;
        }else if(row==1){
            cell.title.text=@"严重度:";
            cell.text.text = self.pRecordM.SEVERITY;
            return cell;
        }else if (row==2){
            cell.title.text=@"发送时间:";
            cell.text.text = self.beginTime;
            return cell;
        } else {
            cell.title.text=@"关闭时间:";
            cell.text.text = self.endTime;
            return cell;
        }
    } else {
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.title.text=@"说  明:";
        cell.text.text = self.pRecordM.PROCESS_DESC;
        cell.isEdit = NO;
        return cell;
    }
}

/*
 *点击方法
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    WEAKSELF
//    long row=indexPath.row;
//    long section=indexPath.section;
    //发送形式
//    if(section==0&&row==5){
//        NSNumber *ninit = [NSNumber numberWithInteger:0];
//        [ActionSheetStringPicker showPickerWithTitle:nil rows:@[testArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
//            //得到选择下标
//            int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
//            DefaineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//            cell.text.text = testArr[iv];
//            
//            if ([testArr[iv] isEqualToString:@"工作联系单"]) {
//                self.sendType = SEND_TYPE_CONTACTLIST;
//            } else if ([testArr[iv] isEqualToString:@"监理通知单"]) {
//                self.sendType = SEND_TYPE_SUPERVISIONNOTICE;
//            } else if ([testArr[iv] isEqualToString:@"工程暂停令"]) {
//                self.sendType = SEND_TYPE_STOPLIST;
//            } else {
//                self.sendType = SEND_TYPE_NULL;
//            }
//            //            [weakSelf.tableView reloadData];
//        } cancelBlock:nil origin:self.view];
//    }
}


/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
#pragma mark - 2017.3.21改 通过流程id得到流程记录信息
- (NSDictionary *)queryProcessByProcessID:(NSString *)processId{
    
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sqlStr=[NSString stringWithFormat:@"select * from process where id='%@'",processId];
    
    FMResultSet *result = [db executeQuery:sqlStr];
    NSDictionary *dic=[NSDictionary dictionary];
    while ([result next]) {
        dic=[result resultDictionary];
    }
    return dic;
}

@end
